package com.yukefms.message;

import com.yukefms.jointpoint.BodyPointCloud;
import com.yukefms.jointpoint.Joint;

/**
 * @Description
 * @Author Ping
 * @Date 2021/6/25 15:27
 **/
public class MessageTools {
    public static final String SEQ_SIGN = "#";
    public static final String JOINT_SEQ_SIGN = "\\+";
    public static final String POSI_SEQ_SIGN = ",";

    /**
     * 	The structure of message received from Camera
     * 		MSG_FORMATE:
     * 			<msg> ::=  <bId> '#' <clock> '#' <payload>
     * 		<payload> ::=  Joint
     * 					|   <payload>  '+'  <payload>
     * 		  <Joint> ::=  <type>','x','y','z','<state>
     * @param message
     * @return bodyCloud
     */
    public static BodyPointCloud toPointCloud(String message) {
        // for test
//        if (!message.contains(JOINT_SEQ_SIGN)) {
//System.out.println("[MessageTools] for debug") ;
//        //    System.out.println(message);
//            return null ;
//        }
        String[] seg_tokens = message.split(SEQ_SIGN) ;
      //  System.out.println(seg_tokens[0] + ", "+ seg_tokens[1]) ;
        String[] payload_tokens = seg_tokens[2].split(JOINT_SEQ_SIGN) ;
        BodyPointCloud pointCloud = new BodyPointCloud(seg_tokens[0]) ;
        String posi[] = null ;
        for (String payload : payload_tokens) {
            posi = payload.split(POSI_SEQ_SIGN) ;
            if (posi.length < 5) {
                try {
                    throw new IllegalArgumentException("Joint arguments need five but four.");
                } catch(IllegalArgumentException e) {
                    continue ;
                }

            }
            pointCloud.addJoint(new Joint(payload.split(POSI_SEQ_SIGN))) ;
        }
        return pointCloud ;


    }
}
